﻿Imports TyranntUserDatabase
Imports TyranntEngine

Public Class UserRoutines

    Private udb As New UserDatabaseAccess
    Private _naz As New Nabaztag

    Public Function Login(ByVal username As String, ByVal password As String) As String
        Dim userID As Integer = udb.GetUserID(username, password)
        If userID < 0 Then
            Return GetErrorCode(userID, "GetUserID")
        End If
        Dim token As String = udb.GenerateToken(userID)
        Return token
    End Function

    Public Function Logoff(ByVal tokenString As String) As String
        Dim userID As Integer = udb.GetUserIDFromToken(tokenString)
        If userID < 0 Then
            Return GetErrorCode(userID, "GetUserIDFromToken")
        Else
            Dim username As String = udb.GetUsername(userID)
            Dim userAlias As String = udb.GetUserAlias(userID)
            udb.DeleteToken(userID)
            Return ""
        End If
    End Function

    Public Function register(ByVal username As String, ByVal password As String, ByVal userAlias As String, ByVal email As String) As String
        Dim group As Integer
        If username = "admin" Then
            group = 1
        Else
            group = 2
        End If
        Return udb.AddUser(username, password, userAlias, email, group)
    End Function

    Public Function ChangePassword(ByVal token As String, ByVal userID As Integer, ByVal newPassword As String) As String
        Dim uid As Integer = udb.GetUserIDFromToken(token)
        If uid < 0 Then
            Return GetErrorCode(userID, "GetUserIDFromToken")
        End If
        If IsAdmin(uid) = False Then
            Return GetErrorCode(-3, "ChangePassword")
        End If

        Return udb.ChangePassword(userID, newPassword)
    End Function

    Public Function GetUserGroup(ByVal tokenString As String) As IntegerType
        Dim result As New IntegerType
        result.IntegerValue = udb.GetUserGroup(tokenString)
        If result.IntegerValue < 0 Then
            result.ErrorMessage = GetErrorCode(result.IntegerValue, "GetUserGroup")
        Else
            result.ErrorMessage = ""
        End If
        Return result
    End Function

    Public Function GetUserAlias(ByVal tokenString As String) As String
        Return udb.GetUserAlias(tokenString)
    End Function

    Public Function GetUserList(ByVal tokenString As String) As String
        ' Pass in the user group to determin what type of list to get
        Return udb.GetUserList(udb.GetUserGroup(tokenString))
    End Function

    Public Function RemoveUser(ByVal tokenString As String, ByVal userID As Integer) As String
        Dim myUserID As Integer = udb.GetUserIDFromToken(tokenString)
        If myUserID < 0 Then
            Return GetErrorCode(myUserID, "GetUserIDFromToken")
        End If
        If IsAdmin(userID) = True Then
            Return udb.RemoveUser(userID)
        Else
            Return GetErrorCode(-3, "RemoveUser")
        End If

    End Function

    Public Function ChangeUserGroup(ByVal tokenString As String, ByVal userID As Integer, ByVal newUserGroup As Integer) As String
        Dim myUserID As Integer = udb.GetUserIDFromToken(tokenString)
        If myUserID < 0 Then
            Return GetErrorCode(myUserID, "GetUserIDFromToken")
        End If
        If IsAdmin(myUserID) = True Then
            Return udb.ChangeUserGroup(userID, newUserGroup)
        Else
            Return GetErrorCode(-3, "RemoveUser")
        End If
    End Function

    Private Function IsAdmin(ByVal userID As Integer) As Boolean
        Dim userGroup As Integer = udb.GetUserGroup(userID)
        If userGroup = 1 Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function GetErrorCode(ByVal result As Integer, ByVal functionName As String) As String
        Select Case result
            Case -1
                Return "[#]usernotexist[" + functionName + "]"
            Case -2
                _naz.SendMessage("fatal error", "Fatal error in routine " + functionName + " exception caught", Nabaztag.VoicesEnum.UK_Edwin)
                Return "[#]failed[" + functionName + "] - " + udb.LastError.ToString
            Case -3
                Return "[#]permissiondenied[" + functionName + "]"
            Case Else
                Return "[#]unknowncode[" + result.ToString + "]"
        End Select
    End Function

    Public Function SetMOTD(ByVal tokenString As String, ByVal message As String) As String
        Dim myUserID As Integer = udb.GetUserIDFromToken(tokenString)
        If myUserID < 0 Then
            Return GetErrorCode(myUserID, "GetUserIDFromToken")
        End If
        Return udb.SetMOTD(message)
    End Function

End Class
